home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3472 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  81 lines

  1. Path: gryphon.phoenix.net!usenet
  2. From: brucew@phoenix.net (Bruce Wedding)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie - I'm stuck
  5. Date: Mon, 29 Jan 1996 06:01:10 GMT
  6. Organization: BranPaul Systems
  7. Message-ID: <4ehlit$r21@gryphon.phoenix.net>
  8. References: <mhoward-2701961816570001@port6.sniff.smallmedia.com>
  9. NNTP-Posting-Host: dial21.phoenix.net
  10. X-Newsreader: Moe's Newsreader    
  11.  
  12. mhoward@plainfield.bypass.com (Mark Howard) wrote:
  13.  
  14. > I've stepped through
  15. >this a bunch of times in the debugger but I'm still clueless!
  16.  
  17. When you were in the debugger, did you have awatch on the value of 
  18. cntr1?
  19.  
  20.  
  21. >        char           halfbyt[8];                             
  22.                                         ^^^ See this 8 ?
  23.  
  24. >        while(bitCntr < 32)
  25. >        {
  26.                                    ^^^^ See this 32?
  27. >                halfbyt[cntr1] = NULL;  
  28. >                for(cntr2 = 0; cntr2 < 4; cntr2 ++)
  29. >                {
  30. >                        if((num & (1 << bitCntr)) != 0)   
  31. >                                /* set corresponding bit in halfbyt[] */
  32. >                                halfbyt[cntr1] |= (1 << cntr2); 
  33.                                                ^^^^ See this used as
  34.  
  35.                                      an index in this size 8 array
  36. >                        bitCntr ++;
  37. >                }
  38. >                cntr1 ++;
  39.                   ^^^^^^^ See this increment 32 times
  40. >        }
  41.  
  42. Here is a fixed version in more ways then I've pointed out.
  43.  
  44. #include <stdio.h>
  45.  
  46. int main(void)
  47. {
  48.    char line[20];
  49.    unsigned long  num;    /* our long int to split */
  50.    int i, j, bitCntr;     /* counters */
  51.    char halfbyt[8] = {0};   
  52.                            
  53.    printf("Enter a number: ");  /* Get a num */
  54.    fgets(line, sizeof(line), stdin);
  55.    sscanf(line, "%lu", &num);
  56.    
  57.    printf("You Entered (decimal) --> %lu\n", num);
  58.    printf("You Entered (hex) --> %#.8lx\n", num); 
  59.         
  60.    i = 0;
  61.    bitCntr = 0;
  62.    for ( i = 0; i < 8; i++)
  63.    {
  64.       for(j = 0; j < 4; j ++)
  65.       {
  66.         if((num & (1L << bitCntr)))   
  67.              halfbyt[i] |= (1L << j);
  68.       bitCntr++;
  69.       }
  70.    }
  71.    printf("Split:\n");
  72.    for(i = 0; i < 8; i ++)
  73.       printf("Half Byte %d --> %#x\n", i, halfbyt[i]);
  74.    return 0;
  75. }
  76.  
  77. Bruce D. Wedding                        Have Compiler, Will Travel!
  78.               Perspicacious Programming Performed Promptly
  79. Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
  80.  
  81.